package com.example.nick.bodyapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.nick.bodyapplication.dao.MyStudent;
import com.example.nick.bodyapplication.dao.MyStudentDao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GreenDaoActivity2 extends AppCompatActivity {

    private TextView tv_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_green_dao);
        tv_title = findViewById(R.id.tv_title);
    }

    /**
     * 插入1w条数据
     */
    private void greenDaoInsert() {
        ArrayList<MyStudent> mySportsList = new ArrayList();
        for (int i =0; i< 10; i++) {
            MyStudent mySports = new MyStudent("name:"+i, i);
            mySportsList.add(mySports);
        }
        long startTime = System.currentTimeMillis();
        App.getmDaoSession().getMyStudentDao().insertInTx(mySportsList);
        long endTime = System.currentTimeMillis();
        long takeTime = endTime - startTime;
        tv_title.setText("greendao插入1w条数据耗时:"+takeTime);
    }

    /**
     * 查询数据
     */
    public void greenDaoQuery() {
        long startTime = System.currentTimeMillis();
        List<MyStudent> datas = App.getmDaoSession().getMyStudentDao().queryBuilder().list();
        long endTime = System.currentTimeMillis();
        long takeTime = endTime - startTime;
        tv_title.setText("greendao查询1w条数据耗时:" + takeTime + "ms, " + datas.get(0).toString()+", "+datas.get(1).toString());
    }

    /**
     * 更新数据
     */
    public void greenDaoUpdate() {
        List<MyStudent> datas = App.getmDaoSession().getMyStudentDao().queryBuilder().list();
        MyStudent myStudent = datas.get(0);
        myStudent.setScore(22f);
        long startTime = System.currentTimeMillis();
        App.getmDaoSession().getMyStudentDao().update(myStudent);
        long endTime = System.currentTimeMillis();
        long takeTime = endTime - startTime;
        tv_title.setText("greendao更新1w条数据耗时:" + takeTime + "ms, myStudent:"+myStudent.toString());
    }

    /**
     * 删除
     * @param
     * @param
     */
    public void greenDaoDelete(String name) {
        //val qb = getSocketCountdownBeanDao().queryBuilder()
        //        val list = qb.where(qb.and(SocketCountdownBeanDao.Properties.DeviceId.eq(bean.deviceId),
        //                SocketCountdownBeanDao.Properties.SubDeviceId.eq(bean.subDeviceId)),
        //                SocketCountdownBeanDao.Properties.CountdownSec.eq(bean.countdownSec)).orderAsc(SocketCountdownBeanDao.Properties.Id).list()
        MyStudentDao myStudentDao = App.getmDaoSession().getMyStudentDao();
        List<MyStudent> list = myStudentDao.queryBuilder().where(MyStudentDao.Properties.Name.eq(name)).list();
        if (list.size() > 0) {
            App.getmDaoSession().getMyStudentDao().delete(list.get(0));
        }
        //App.getmDaoSession().getMySportsDao().delete(mySports);
        //App.getmDaoSession().getMySportsDao().deleteAll();
    }

    public void onInsert(View view) {
        greenDaoInsert();
    }
    public void onQuery(View view) {
        greenDaoQuery();
    }
    public void onUpdate(View view) {
        greenDaoUpdate();
    }
    public void onDelete(View view) {
        List<MyStudent> datas = App.getmDaoSession().getMyStudentDao().queryBuilder().list();
        MyStudent myStudent = datas.get(1);
        greenDaoDelete(myStudent.getName());
    }
}